/*javascript document*/

function display(tdid, message) {

	//controlid.setAttribute("class", errorclass);
	tdid.innerHTML = message;

}

function checkBlank(controlid) {
	var valid=false;
	if (controlid.value != "") {
		valid=true;
	}
	return valid;
}

function checkEmail(control) {
	var valid = true;
	if (!control.value.match(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/)) {
		valid = false;
	}
	return valid;
}

function loginValidate(){
	var username=document.getElementById("username");
	var password=document.getElementById("password");
	var tdid=document.getElementById("tderror");
	var blank=checkBlank(username) && checkBlank(password);
	if(!blank){
		display(tdid,"Username and password required");
		return false;
	} else if(!checkEmail(username)){
		display(tdid,"Invalid Username");
		return false;
	}
	return true;
}

function postQuestionForm() {
	var titleName = document.getElementById("titlename");
	var description = document.getElementById("description");
	var tagnames = document.getElementById("tagnames");
	TrimString(titleName);
	TrimString(description);
	TrimString(tagnames);
	var errorspan1= document.getElementById("errorspan1");
	var errorspan2=document.getElementById("errorspan2");
	var errorspan3=document.getElementById("errorspan3");
	var valid = true;
	if (!checkBlank(titleName)) {
		display(errorspan1,"Question Title required");
		valid =false;
	} else {
		display(errorspan1,"");
	}
	if (!checkBlank(description)) {
		display(errorspan2,"Question Discription required");
		valid =false;
	} else {
		display(errorspan2,"");
	}
	if (!checkBlank(tagnames)) {
		display(errorspan3,"Question Tags required");
		valid =false;
	} else {
		display(errorspan3,"");
	}
	return valid;

}

function approveAnswerForm() {
	var radios = document.getElementsByTagName('input');
	for (var i = 0; i < radios.length; i++) {
	    if (radios[i].type === 'radio' && radios[i].checked) {
	        // get value, set checked flag or do whatever you need to
	        value = radios[i].value;  
	        return true;
	    }
	}
	alert("Please select answer");
	return false;
}


function answerForm() {
	TrimString(document.getElementById("answerdescription"));
	if(document.getElementById("answerdescription").value == "") {
			alert("Please Enter Answer...!!");
			return false;
		}
	return true;
}

function TrimString(str)
{
	str.value = str.value.replace(/^\s+/,""); //Left trim        
	str.value = str.value.replace(/\s+$/,""); //Right trim
}
